| Conditions | 1 |
| Paths | 4 |
| Total Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* globals Ziggurat */ |
||
| 15 | function(prettyNumber, $sce, data) { |
||
| 16 | this.gaussian = new Ziggurat(); |
||
| 17 | |||
| 18 | /* Return the HTML representation of an element, or the element itself |
||
| 19 | if it doesn't have one */ |
||
| 20 | this.getHTML = function(resource) { |
||
| 21 | let html = data.html[resource]; |
||
| 22 | if (typeof html === 'undefined' && data.resources[resource]) { |
||
| 23 | html = data.resources[resource].html; |
||
| 24 | } |
||
| 25 | if (typeof html === 'undefined') { |
||
| 26 | return resource; |
||
| 27 | } |
||
| 28 | return html; |
||
| 29 | }; |
||
| 30 | |||
| 31 | this.prettifyNumber = function(number) { |
||
| 32 | if (typeof number === 'undefined' || number === null) { |
||
| 33 | return null; |
||
| 34 | } |
||
| 35 | if (number === '') { |
||
| 36 | return ''; |
||
| 37 | } |
||
| 38 | if (number === Infinity) { |
||
| 39 | return '∞'; |
||
| 40 | } |
||
| 41 | if (number === 0) { |
||
| 42 | return '0'; |
||
| 43 | } |
||
| 44 | if (number > 1e6) { |
||
| 45 | // Very ugly way to extract the mantisa and exponent from an exponential string |
||
| 46 | let exponential = number.toPrecision(6).split('e'); |
||
| 47 | let exponent = parseFloat(exponential[1].split('+')[1]); |
||
| 48 | // And it is displayed with superscript |
||
| 49 | return Number.parseFloat(exponential[0]).toFixed(4) + |
||
| 50 | ' × 10<sup>' + |
||
| 51 | this.prettifyNumber(exponent) + |
||
| 52 | '</sup>'; |
||
| 53 | } |
||
| 54 | if (number < 1e-6) { |
||
| 55 | // Very ugly way to extract the mantisa and exponent from an exponential string |
||
| 56 | let exponential = number.toPrecision(6).split('e'); |
||
| 57 | let exponent = parseFloat(exponential[1].split('-')[1]); |
||
| 58 | // And it is displayed with superscript |
||
| 59 | return Number.parseFloat(exponential[0]).toFixed(4) + |
||
| 60 | ' × 10<sup>-' + |
||
| 61 | this.prettifyNumber(exponent) + |
||
| 62 | '</sup>'; |
||
| 63 | } |
||
| 64 | // we use a regex to remove trailing zeros, plus . (if necessary) |
||
| 65 | return prettyNumber(number, 6); |
||
| 66 | }; |
||
| 67 | |||
| 68 | this.randomDraw = function(number, p) { |
||
| 69 | let production; |
||
| 70 | let mean = number * p; |
||
| 71 | // Gaussian distribution |
||
| 72 | let q = 1 - p; |
||
| 73 | let variance = number * p * q; |
||
| 74 | let std = Math.sqrt(variance); |
||
| 75 | production = Math.round(this.gaussian.nextGaussian() * std + mean); |
||
| 76 | if (production > number) { |
||
| 77 | production = number; |
||
| 78 | } |
||
| 79 | if (production < 0) { |
||
| 80 | production = 0; |
||
| 81 | } |
||
| 82 | return production; |
||
| 83 | }; |
||
| 84 | |||
| 85 | this.trustHTML = function(html) { |
||
| 86 | return $sce.trustAsHtml(html); |
||
| 87 | }; |
||
| 88 | } |
||
| 89 | ]); |
||
| 90 |